package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.*;
import com.cskaoyan.shopping.service.cache.CacheManager;
import com.cskaoyan.shopping.service.cache.ShoppingCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author :LuoXuXin & ChengWenqi
 * @version : $version
 * @date :Created in 2022/4/22   15:05
 * @description:
 * @return:
 */
@RestController
@RequestMapping("shopping")
public class ShoppingController {

    /*@Autowired
    ShoppingService shoppingService;*/

    @Autowired
    ShoppingCacheManager shoppingCacheManager;

    @Autowired
    IHomeService iHomeService;

    @Autowired
    IContentService iContentService;

    @Autowired
    IProductCateService iProductCateService;

    @Autowired
    IProductService iProductService;

    /**
     * 获取商城首页
     * 创建日期：2022/04/24 12:41
     * @param
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("homepage")
    public ResponseData shoppingHomePage() {
        // 先获取缓存
        Object cache = shoppingCacheManager.checkCache(GlobalConstants.HOMEPAGE_CACHE_KEY, GlobalConstants.HOMEPAGE_CACHE_KEY);
        // 如果获取到了，直接返回
        if (cache != null) {
            return new ResponseUtil<>().setData((cache));
        }
        // 如果没获取到，调用service里的方法，从数据库获取数据，并放到缓存里
        HomePageResponse response = iHomeService.homepage();

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            Set<PanelDto> panelContentItemDtos = response.getPanelContentItemDtos();
            shoppingCacheManager.setCache(GlobalConstants.HOMEPAGE_CACHE_KEY, GlobalConstants.HOMEPAGE_CACHE_KEY, panelContentItemDtos, GlobalConstants.HOMEPAGE_EXPIRE_TIME);
            return new ResponseUtil().setData(panelContentItemDtos);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 获取商城首页导航栏
     * 创建日期：2022/04/24 12:58
     * @param
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("navigation")
    public ResponseData shoppingNavigation() {
        // 先获取缓存
        Object cache = shoppingCacheManager.checkCache(GlobalConstants.HEADER_PANEL_CACHE_KEY, GlobalConstants.HEADER_PANEL_CACHE_KEY);
        // 有缓存，直接返回
        if (cache != null) {
            return new ResponseUtil<>().setData(cache);
        }
        // 没有缓存，调用 service 方法，获取数据库，放入缓存
        NavListResponse response = iContentService.queryNavList();

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            List<PanelContentDto> pannelContentDtos = response.getPannelContentDtos();
            shoppingCacheManager.setCache(GlobalConstants.HEADER_PANEL_CACHE_KEY, GlobalConstants.HEADER_PANEL_CACHE_KEY, pannelContentDtos, GlobalConstants.HEADER_PANEL_EXPIRE_TIME);
            return new ResponseUtil().setData(pannelContentDtos);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 获取所有品类信息
     * 创建日期：2022/04/24 14:36
     * @param  request 排序
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("categories")
    public ResponseData shoppingCategories(AllProductCateRequest request) {
        // 先获取缓存
        Object cache = shoppingCacheManager.checkCache(GlobalConstants.PRODUCT_CATE_CACHE_KEY, GlobalConstants.PRODUCT_CATE_CACHE_KEY);
        // 有，直接返回
        if (cache != null) {
            return new ResponseUtil<>().setData(cache);
        }
        // 没有，从数据库获取，放进缓存
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            List<ProductCateDto> productCateDtoList = response.getProductCateDtoList();
            shoppingCacheManager.setCache(GlobalConstants.PRODUCT_CATE_CACHE_KEY, GlobalConstants.PRODUCT_CATE_CACHE_KEY, productCateDtoList, GlobalConstants.PRODUCT_CATE_EXPIRE_TIME);
            return new ResponseUtil().setData(productCateDtoList);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 根据商品id，获得商品详情
     * 创建日期：2022/04/24 15:15
     * @param  id 商品id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("product/{id}")
    public ResponseData shoppingProduct(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        // 先获取缓存
        Object cache = shoppingCacheManager.checkCache(GlobalConstants.PRODUCT_ITEM_CACHE_KEY, id);
        // 有，直接返回
        if (cache != null) {
            return new ResponseUtil<>().setData(cache);
        }
        // 没有，从数据库获取，放进缓存
        ProductDetailResponse response = iProductService.getProductDetail(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            ProductDetailDto productDetailDto = response.getProductDetailDto();
            shoppingCacheManager.setCache(GlobalConstants.PRODUCT_ITEM_CACHE_KEY, id, productDetailDto, GlobalConstants.PRODUCT_ITEM_EXPIRE_TIME);
            return new ResponseUtil().setData(productDetailDto);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }



    /**
     * 获取分页商品信息
     * 创建日期：2022/04/24 20:16
     * @param  request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("goods")
    public ResponseData shoppingGoods(AllProductRequest request) {
        AllProductResponse response = iProductService.getAllProduct(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getData());
            pageResponse.setTotal(response.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 获取热门推荐商品
     * 创建日期：2022/04/24 21:26
     * @param
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author ChengWenqi
     */
    @GetMapping("recommend")
    public ResponseData shoppingRecommend() {
        // 先获取缓存
        Object cache = shoppingCacheManager.checkCache(GlobalConstants.RECOMMEND_PANEL_CACHE_KEY, GlobalConstants.RECOMMEND_PANEL_CACHE_KEY);
        // 有，直接返回
        if (cache != null) {
            return new ResponseUtil<>().setData(cache);
        }
        // 没有，从数据库获取，放进缓存
        RecommendResponse response = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            Set<PanelDto> panelContentItemDtos = response.getPanelContentItemDtos();
            shoppingCacheManager.setCache(GlobalConstants.RECOMMEND_PANEL_CACHE_KEY, GlobalConstants.RECOMMEND_PANEL_CACHE_KEY, panelContentItemDtos, GlobalConstants.RECOMMEND_CACHE_EXPIRE);
            return new ResponseUtil().setData(panelContentItemDtos);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
